package com.eddie.stack;

import java.util.Stack;

import static com.eddie.stack.PolandNotation.cal;
import static com.eddie.stack.PolandNotation.priority;

public class Calculator {
//
    public static void main(String[] args) {
        Calculator calculator = new Calculator();
        //创建表达式
        String expression = "3/2";
        int extracted = calculator.calculate(expression);
        System.out.println(extracted);
    }

    public int extracted(String expression) {
        //创建两个栈，数栈和符号栈
        ArrayStack2 numStack = new ArrayStack2(expression.length());
        ArrayStack2 oprStack = new ArrayStack2(expression.length());
        //定义需要的相关变量
        int num1;
        int num2;
        int opr;
        int result;
        StringBuilder keepNum;
        //开始while语句循环扫描expression
        char[] charArray = expression.toCharArray();
        for (int i = 0, charArrayLength = charArray.length; i < charArrayLength; i++) {
            char c = charArray[i];
            if (oprStack.isOpr(c)) {
                if (!oprStack.isEmpty()) {
                    //如果符号栈有操作符，就比较，如果当前
                    if (oprStack.priority(c) <= oprStack.priority(oprStack.peek())) {
                        num1 = numStack.pop();
                        num2 = numStack.pop();
                        opr = oprStack.pop();

                        result = numStack.cal(num1, num2, opr);
                        numStack.push(result);
                    }
                }
                oprStack.push(c);
            } else {
                //ASCII码1=49，3=51，如果传入char类型的1相当于传入49 减去48则是正确的
                keepNum = new StringBuilder(String.valueOf(c - 48));
                    while (i < charArray.length - 1&&!numStack.isOpr(charArray[i + 1])) {
                        keepNum.append(charArray[i + 1] - 48);
                        i++;
                    }
                numStack.push(Integer.parseInt(keepNum.toString()));
            }
        }
        while (!oprStack.isEmpty()) {
            num1 = numStack.pop();
            num2 = numStack.pop();
            opr = oprStack.pop();
            result = numStack.cal(num1, num2, opr);
            numStack.push(result);
        }
        return numStack.pop();
    }

    public int calculate(String s) {
        Stack<Integer> s1 =  new Stack<Integer>();
        Stack<String> s2 =  new Stack<String>();
        int num1;
        int num2;
        char opr;
        int result;
        char[] arr = s.toCharArray();
        for (char c : arr) {
            if (priority(c) == -1) {
                s1.push(c - 48);
            } else {
                if (s2.size() > 0 && priority(c) <= priority(s2.peek().charAt(0))) {
                    num1 = s1.pop();
                    num2 = s1.pop();
                    opr = s2.pop().charAt(0);
                    result = cal(num1, num2, opr);
                    s1.push(result);
                }
                s2.push(String.valueOf(c));
            }
        }
        while(s2.size() != 0) {
            num1 = s1.pop();
            num2 = s1.pop();
            opr = s2.pop().charAt(0);
            result = cal(num1, num2, opr);
            s1.push(result);
        }
        return s1.pop();
    }

}
